///Jays screen shot application for XP. Written 11-5-2010
///You may modify code in anyway you see fit as long as you distribute the source code with your application.
///You may not charge for this software for this application is open source for all to use and learn from.
///If you make modifications to the code, i would like it if you sent me the code as well.
///My email address is Jay@MissoulaSoftware.com. 
///I would like to see this application turn into a technical writing application that allows you to embed screenshots into a html page. 
///If you would like to work on this as a team, feel free to contact me.
///


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.Windows;
using HotKey;

namespace ScreenshotCapturer
{
    public partial class Form1 : Form
    {

        private static Bitmap bmpScreenshot;
        private static Graphics gfxScreenshot;
        bool closeme = false;
        
        public Form1()
        {
            InitializeComponent();
          
            this.Hide();
        }

        private void saveScreenshot_FileOk(object sender, CancelEventArgs e)
        {

        }
        
        private static Image cropImage(Image img, Rectangle cropArea)
        {
            Bitmap bmpImage = new Bitmap(img);
            Bitmap bmpCrop = bmpImage.Clone(cropArea,
            bmpImage.PixelFormat);
            return (Image)(bmpCrop);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
           
            notifyIcon1.BalloonTipTitle = "Did You Know?";
            notifyIcon1.BalloonTipText = "You can capture the screen by pressing CONTROL + ALT + S anytime!";
            notifyIcon1.ShowBalloonTip(3000);
            HotKeyManager.RegisterHotKey(Keys.S, KeyModifiers.Alt | KeyModifiers.Control );
            HotKeyManager.HotKeyPressed += new EventHandler<HotKeyEventArgs>(HotKeyManager_HotKeyPressed);
            
        }
        delegate void del();
        void HotKeyManager_HotKeyPressed(object sender, HotKeyEventArgs e)
        {   del d =  delegate{
            toolStripButton1.PerformClick();};
        Invoke(d);
        }
        
        
        private void toolStripButton1_Click(object sender, EventArgs e)
        { 
            this.Hide();
            Form openform = new Form();
            foreach (Form f in Application.OpenForms)
            {
                if (f is ShotCropper)
                {
                    openform = f;
                }
            }
            try
            {
                ((ShotCropper)openform).Close();
            }
            catch
            { }
            // Hide the form so that it does not appear in the screenshot
           

            // Set the bitmap object to the size of the screen
            bmpScreenshot = new Bitmap(Screen.FromControl(this).Bounds.Width, Screen.FromControl(this).Bounds.Height, PixelFormat.Format32bppArgb);
            // Create a graphics object from the bitmap
            gfxScreenshot = Graphics.FromImage(bmpScreenshot);
            // Take the screenshot from the upper left corner to the right bottom corner
            gfxScreenshot.CopyFromScreen(Screen.FromControl(this).Bounds.X, Screen.FromControl(this).Bounds.Y, 0, 0, Screen.FromControl(this).Bounds.Size, CopyPixelOperation.SourceCopy);
            // Save the screenshot to the specified path that the user has chosen
            bmpScreenshot.Save("temp.png", ImageFormat.Png);
            // Show the form again
            ShotCropper sc = new ShotCropper();
            
            sc.Show();
            sc.WindowState = FormWindowState.Maximized;
            this.Show();
            this.TopMost = true;
        }

        private void captureToolStripMenuItem_Click(object sender, EventArgs e)
        {
            toolStripButton1.PerformClick();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            
            if (MessageBox.Show("Are you sure you would like to exit?", "Confirm Exit", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                closeme = true;
                this.Close();
            }
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            
            if (closeme == true)
            {
                Application.Exit();
            }
            else
            {
                this.Hide();
                e.Cancel = true;
            }
        }

        private void notifyIcon1_DoubleClick(object sender, EventArgs e)
        {
            this.Show();
        }

        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            About a = new About();
            a.Show();
        }

        private void toolStripLabel1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("XP Snipping Tool. Created By Jay Stratemeyer - Jay@MissoulaSoftware.com" + Environment.NewLine +
                "This software is free for public use and commercial use. Buuut, " + Environment.NewLine +
                "I really could use some change for a coffee! :)" + Environment.NewLine +
            "Please donate at http://xpsnipping.codeplex.com and send me a suggestion. It may just be in the next build!", "About XP Snipping Tool");
        }

        private void toolStripLabel2_Click(object sender, EventArgs e)
        {
            MessageBox.Show("CONTROL + ALT + S = start snipping!", "Hot Keys");
        }

       
    }
}